package com.jackeysun.log

import android.content.Context
import java.io.File

/**
 *   Created by sunxunchao on 2021/12/10.
 */


object LogConfig {


    var toWrite = false
    var useShake : Boolean = false
    var file: File? = null

//    private var logName = "log.log"
//    private var crashName = "crash.log"

    var fileSize = 100000L //日志文件的大小，默认0.1M
    var saveFileTime = 1 * 24 * 60 * 60 * 1000L //一天
    var versionName: String = "1.0.0"
    var versionCode: Int = 0
    var onLogcatListener: OnLogcatListener? = null

    fun toUseShake(useShake : Boolean):LogConfig{
        this.useShake = useShake
        return this
    }
    fun toWrite(toWrite: Boolean): LogConfig {
        this.toWrite = toWrite
        return this
    }

    fun fileSize(size: Long): LogConfig {
        this.fileSize = size
        return this
    }

    fun saveFileTime(saveFileTime: Long): LogConfig {
        this.saveFileTime = saveFileTime
        return this
    }

    fun versionName(versionName: String): LogConfig {
        this.versionName = versionName
        return this
    }

    fun versionCode(versionCode: Int): LogConfig {
        this.versionCode = versionCode
        return this
    }

    fun builder(context: Context) {
        if (useShake){
            SensorHelper(context)
        }
        val file = File(context.getExternalFilesDir("${File.separator}log")?.absolutePath!!)
        this.file = file
        deleteOverdueFile(file)
        if (!file.exists()) {
            file.mkdirs()
        }
        if (toWrite) {
            CrashHelper(file)
            LogHelper(file)
        }
    }

}

public interface OnLogcatListener {
    fun onLogcatInfo(log: String)
}